class image_vm
   constructor: (inits) ->
     @source = ko.observable ""
     @selected = ko.observable false       

class image_pair_vm
  constructor: (inits) ->
    @images = ko.observableArray []
       
class page_vm
  constructor: (inits) ->
    @rows = ko.observableArray []
    
class window.select_image_vm
  constructor: (@parent) ->    
    @imagePages = ko.observableArray []
    @images = []
    
    @selectedImage = ko.observable 'images/43vhFHg.jpg'   
    
    window.setTimeout =>  
      @_Create_Images()
    , 250
    
  _Create_Images: =>
    @images.push new image_vm for n in [0..11]
    @images[0].source 'images/43vhFHg.jpg'    
    @images[1].source 'images/5HnLkdv.jpg'
    @images[2].source 'images/BFTLjk1.jpg'
    @images[3].source 'images/cat-on-invisible-bike.jpg'
    @images[4].source 'images/cIe9r.jpg'
    @images[5].source 'images/corgilaforge.jpg'
    @images[6].source 'images/grumpycat,jpg.jpg'
    @images[7].source 'images/JLBC8u7.jpg'
    @images[8].source 'images/JmtpsC4.jpg'
    @images[9].source 'images/JPy8ul.jpg'
    @images[10].source 'images/kT2QBoO.jpg'
    @images[11].source 'images/startrekdog.jpg'
    
    for n in [0..11]
      @images[n].selected (@images[n].source() == @selectedImage()) 
    
    underlying_array = @imagePages()
    underlying_array.push new page_vm() for n in [0..1]
    underlying_array[0].rows().push new image_pair_vm for n in [0..2]
    underlying_array[1].rows().push new image_pair_vm for n in [0..2]
  
    underlying_array[0].rows()[0].images()[0] = @images[0]
    underlying_array[0].rows()[0].images()[1] = @images[1]
    underlying_array[0].rows()[1].images()[0] = @images[2]
    underlying_array[0].rows()[1].images()[1] = @images[3]
    underlying_array[0].rows()[2].images()[0] = @images[4]
    underlying_array[0].rows()[2].images()[1] = @images[5]
    
    underlying_array[1].rows()[0].images()[0] = @images[6]
    underlying_array[1].rows()[0].images()[1] = @images[7]
    underlying_array[1].rows()[1].images()[0] = @images[8]
    underlying_array[1].rows()[1].images()[1] = @images[9]
    underlying_array[1].rows()[2].images()[0] = @images[10]
    underlying_array[1].rows()[2].images()[1] = @images[11]
       
    @imagePages.valueHasMutated()      
  
  Tap_Image: (d,e) =>    
    d.selected(true)
    @selectedImage d.source()
    for n in [0..11]
      unless d is @images[n]
        @images[n].selected(false)
    @imagePages.valueHasMutated()
        
   Submit_Image: (d,e) =>     
     @parent.current_game().image(@selectedImage())
     @parent.Navigate Templates.HOME
     
   Change_Index: (index, elem) =>
     if index == 0
       $('#pageOne').removeClass("ui-btn-up-a").addClass("ui-btn-up-b");
       $('#pageTwo').removeClass("ui-btn-up-b").addClass("ui-btn-up-a");
     else
       $('#pageOne').removeClass("ui-btn-up-b").addClass("ui-btn-up-a");
       $('#pageTwo').removeClass("ui-btn-up-a").addClass("ui-btn-up-b");
       
   Page_One: (d,e) =>
     window.swipe.slide(0,300)
     
   Page_Two: (d,e) =>
     window.swipe.slide(1,300)
         
   Load_Swype: =>
     console.log "Loading Swype."
     secondPage = false
     for n in [6..11]
       if @images[n].selected()
         secondPage = true
         break
          
     window.swipe = Swipe($('#slider')[0], {callback: this.Change_Index, startSlide: if secondPage then 1 else 0})     
     
     
